const path = require('path')
function createDataServer() {
  const sqlite3 = require("sqlite3").verbose();
  const bodyParser = require("body-parser");
  const db_path = path.join(__dirname, "./database/datafromtjj.sqlite3");
  const sqlite3_db = new sqlite3.Database(db_path);

  const cors = require("cors");
  const express = require("express");
  const app_express = express();
  const data_port = 8888;
  app_express.use(cors());
  app_express.use(bodyParser.json());

  app_express.get("/chinagdp", (req, res) => {
    sqlite3_db.all("SELECT * FROM chinagdp10years ORDER BY year ASC", [], (err, rows) => {
      if (err) {
        res.send(err);
      } else {
        res.send(rows);
      }
    });
  });

  app_express.get("/chinapopulation", (req, res) => {
    sqlite3_db.all("SELECT * FROM chinapopulation10years ORDER BY year ASC", [], (err, rows) => {
      if (err) {
        res.send(err);
      } else {
        res.send(rows);
      }
    });
  });

  app_express.listen(data_port, () => {
    console.log(`Data server running at: http://localhost:${data_port}`);
  });
}

createDataServer()